import telnetlib
from device_info import *
from w2f import w2f
from tn_ready import tn_ready
import time

def tn_show_int_br(device_15,mgmt_ip,username,password):
    tn = tn_ready(device_15,mgmt_ip,username,password)
    tn.write('show ip interface brief'.encode('ascii') + b'\n')
    time.sleep(1)
    status = tn.read_until(device_15.encode('ascii')).decode('ascii')
    tn.close()
    return status

def show_run(device_15,mgmt_ip,username,password):
    tn = tn_ready(device_15,mgmt_ip,username,password)
    tn.write('show run'.encode('ascii') + b'\n')
    time.sleep(1)
    run_config = tn.read_until(device_15.encode('ascii')).decode('ascii')
    tn.close()
    return run_config

def ospf_int(device_15,mgmt_ip,username,password):
    tn = tn_ready(device_15,mgmt_ip,username,password)
    tn.write('show ip ospf interface brief'.encode('ascii') + b'\n')
    time.sleep(1)
    ospf_int = tn.read_until(device_15.encode('ascii')).decode('ascii')
    tn.close()
    return ospf_int

def A(device_15,mgmt_ip,username,password):
    AH = ospf_int(device_15,mgmt_ip,username,password)
    AH = AH.splitlines()
    AH.pop(0);AH.pop(0);AH.pop(-1)
    AH1 = []
    for x in AH:
        x = x.split()
        #print(x)
        AH1.append((x[0],x[2]))
    #print(AH1)
    for a,b in AH1:
        print(device_15 + ' ' + a + ' in ' + 'area ' + b)
    
"""
[
 'show ip ospf interface brief',
 'Interface    PID   Area            IP Address/Mask    Cost  State Nbrs F/C',
 'Vl12         1     0               10.1.112.1/30      1     BDR   1/1',
 'Gi0/0        1     0               10.1.11.2/30       1     DR    1/1',
 'Vl200        1     10              10.1.200.252/24    1     DR    0/0',
 'Vl100        1     10              10.1.100.101/24    1     DR    0/0',
 'SW1#'
 ]

 [
 'Vl12         1     0               10.1.112.1/30      1     BDR   1/1',
 'Gi0/0        1     0               10.1.11.2/30       1     DR    1/1',
 'Vl200        1     10              10.1.200.252/24    1     DR    0/0',
 'Vl100        1     10              10.1.100.101/24    1     DR    0/0'
 ]

['Vl12', '1', '0', '10.1.112.1/30', '1', 'BDR', '1/1']
['Gi0/0', '1', '0', '10.1.11.2/30', '1', 'DR', '1/1']
['Vl200', '1', '10', '10.1.200.252/24', '1', 'DR', '0/0']
['Vl100', '1', '10', '10.1.100.101/24', '1', 'DR', '0/0']
"""

choice = input('what do you want? \n'
               '[1] show run \n'
               '[2] show ip interface brief \n'
               '[3] show ip ospf interface brief \n'
               '[4] AH \n'
               'enter your choice:')

for x in device_info:
    device_15 = x['device_name']+'#'
    mgmt_ip = x['mgmt_ip']
    username = x['username']
    password = x['password']
    try:
        if choice == '1':
            ability = 'show_run'
            syslog = show_run(device_15,mgmt_ip,username,password)
            w2f(device_15[0:-1],syslog,ability)
        elif choice == '2':
            ability = 'show_ip_interface_brief'
            syslog = tn_show_int_br(device_15,mgmt_ip,username,password)
            w2f(device_15[0:-1],syslog,ability)
        elif choice == '3':
            ability = 'show_ip_ospf_interface_brief'
            syslog = ospf_int(device_15,mgmt_ip,username,password)
            w2f(device_15[0:-1],syslog,ability)
        elif choice == '4':
            A(device_15,mgmt_ip,username,password)
        else:
            print('choice error')
    except Exception as e:
        print(device_15,e)
        
